package nc.ui.arap.newbalancequery;

import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import nc.itf.fi.multDataSrc.ICheckChangeDataSrc;
import nc.vo.arap.pub.QryCondVO;
import nc.vo.arap.pub.QueryStructVO;
import nc.vo.pub.lang.UFDate;

public class BalCheckChangeDataSrc implements ICheckChangeDataSrc {

	public Map checkDataSrcChange(Object qryVO, String nodeNo) {
		Map<Object, Object> validValueMap = new HashMap<Object, Object>();
		boolean pastYear = false;
		if (((QueryStructVO) qryVO).getDate() != null) {
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(UFDate
					.getDate(((QueryStructVO) qryVO).getDate()[0]).toDate());
			int startyear = calendar.get(Calendar.YEAR);
			calendar.setTime(UFDate
					.getDate(((QueryStructVO) qryVO).getDate()[1]).toDate());
			int endyear = calendar.get(Calendar.YEAR);
			pastYear = startyear != endyear;
		}
		validValueMap.put("pastYear", pastYear ? "Y" : "N");
		validValueMap.put("corpnum", ((QueryStructVO) qryVO).getCorp().length);

		for (int i = 0; i < ((QueryStructVO) qryVO).getVetQryObj().size(); i ++) {
			genGuideLine(qryVO, validValueMap, i, "fb_hbbm", "hbbmnum");
			genGuideLine(qryVO, validValueMap, i, "fb_cinventoryid", "cinventoryidnum");
			genGuideLine(qryVO, validValueMap, i, "fb_jobid", "jobidnum");
		}
		recacl("hbbmnum", validValueMap);
		recacl("cinventoryidnum", validValueMap);
		recacl("jobidnum", validValueMap);
		return validValueMap;
	}

	private void recacl(String key, Map<Object, Object> validValueMap) {
		if (!validValueMap.containsKey(key)) {
			validValueMap.put(key, 0);
		} else {
			if ((Integer) validValueMap.get(key) == 0) {
				validValueMap.remove(key);
			}
		}
	}

	private void genGuideLine(Object qryVO, Map<Object, Object> validValueMap,
			int i, String objCode, String key) {
		String qryobjCode = ((QueryStructVO) qryVO).getVetQryObj().get(i).getFldorigin() + "_" + ((QueryStructVO) qryVO).getVetQryObj().get(i).getQryfld();
		if (objCode.equals(qryobjCode.toLowerCase())) {
			if (((QueryStructVO) qryVO).getVetQryObj().get(i).getQryCondvos().size() >= 2) {
				for (QryCondVO condvo : ((QueryStructVO) qryVO).getVetQryObj().get(i).getQryCondvos())  {
					Object obj = condvo.getObjValues();
					if (obj != null && obj instanceof List) {
						validValueMap.put(key, ((List<?>) obj).size());
					} else {
						validValueMap.put(key, 0);
					}
				}
			} else {
				validValueMap.put(key, 0);
			}
		}
	}

}
